from Goldenberry.optimization.ga.GbMutatorMgr.MutatorStrategy import MutatorStrategy
import numpy as np
import random as ran

class OneBitBinaryMutation(MutatorStrategy):
    """One bit mutation."""
    def mutate(self, individual, prob):
        """Mutate a individual using one point one bit mutation.
    
         :param individual: GbIndividual to mutate
         :returns: array genotype of the individual generated
         """
        random = ran.random()
        if prob > random:
            genotype = individual.genotype
            size = genotype.shape[0]
            position = ran.randint(0, size-1)        
            genotype[position] = not bool(genotype[position])
        return individual

class MultipleBitBinaryMutation(MutatorStrategy):
    """Multiple bit mutation."""
    def mutate(self, individual, prob):
        """Mutate a individual using multiple point bit mutation.
    
         :param individual: GbIndividual to mutate
         :returns: array genotype of the individual generated
         """
        genotype = individual.genotype
        size = genotype.shape[0]
        probability = prob
        #if probability==0.0:
        #   probability = 1.0 / size
        for x in range(len(genotype)):
            random = ran.random()
            if probability > random:
               genotype[x] = not bool(genotype[x])
        return individual
